exports.MallUI = GL2.Node.subclass(
{
    classname: 'MallUI',
    
    initialize: function(mallModel) 
    {
		this._mallObj = null; //Model Object
		this._floorsUIArray = null;
		this._storesUIArray = null;
		this._touchHandler = null;
		this._gridArray = null;
		this._dummyFloor = null;
		this._storeMainView = null;
		
		this._gridArray = new Array();
		this._floorsUIArray = new GAMutableArray();
		this._storesUIArray = new GAMutableArray();
		
		this._touchHandler = new TouchHandler(this);
		this._touchHandler.handleTouches();
		
		this._mallObj = mallModel;
    },
	
	createView: function()
	{
		this.initializeAllFloors();
		this.initializeAllStores();
		this.handleEmptyShops();		
		ElevatorHandler.instantiate();
		ElevatorHandler.initializeAllElevators();
		
				
		AvatarHandler.instantiate();
		AvatarHandler.initializeDefaultAvatars();
		AvatarHandler.initNewAvatarTimer();
		
		TrashHandler.instantiate();
		TrashHandler.initializeAllTrashes();
		TrashHandler.initNewTrashTimer();		
	},
	
	initializeAllFloors: function() 
	{
		var allFloors = this._mallObj._allFloors;
		var positionY = 0;
		for(var i=0 ; i<allFloors.getLength() ; i++) 
		{
			var floorModel = allFloors.objectAtIndex(i);
			var floorUI = new FloorUI(floorModel);
			positionY = positionY - FLOOR_HEIGHT;
			floorUI.createView();
			floorUI.setDepth(101);
			floorUI.setPosition([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(positionY)]);
			this.addChild(floorUI);
			this._floorsUIArray.addObject(floorUI);
			
			this.addGridRow();
			ElevatorHandler.addMoreElevatorsFloor();
		}
		if(this._floorsUIArray.getLength() > 0)
		{
			this._touchHandler.updateTouchTarget(this._floorsUIArray.objectAtIndex(this._floorsUIArray.getLength()-1).getPosition());
		}
		this.addDummyFloor();
	},
	
	initializeAllStores: function()
	{
		var allStores = this._mallObj._allStores;
		for(var i=0 ; i<allStores.getLength() ; i++) 
		{			
			var storeModel = allStores.objectAtIndex(i);
			var storeUI = new StoreContainer(storeModel);
			storeUI.setContainerPosition();
			storeUI.createView();
			storeUI.setDepth(102);
			this.addChild(storeUI);
			
			this._storesUIArray.addObject(storeUI);
			
			this.reserveGridLocationForObject(storeUI);
		}
	},
	
	handleEmptyShops: function()
	{
		for(var i=0 ; i<this._gridArray.length ; i++)
		{
			for(var j=0 ; j<3 ; j++)
			{
				if(!this._gridArray[i][j])
				{
					var storeModel = new MMStoreModel();
					storeModel.setPosition(new Core.Point(i,j));
					
					var storeUI = new StoreContainer(storeModel);
					storeUI.setContainerPosition();
					storeUI.createView();
					storeUI.setDepth(102);
					this.addChild(storeUI);
					
					this.reserveGridLocationForObject(storeUI);
				}
			}
		}
	},
	
	getStoresUIArray: function()
	{
		return this._storesUIArray;
	},
	
	addStore: function(itemId, position)
	{
		var itemModel = GAGameLoader.getStoreHandler().getStoreObjectForKey(itemId);
		var itemSizeWidth = itemModel.custom_attributes.width;
		
		if(itemSizeWidth > 1)
		{
			position.setY(0);
		}
		
		if(itemId && this._storesUIArray)
		{		
			var storeModel = this._mallObj.addStore(itemId, position);
			
			var storeUI = new StoreContainer(storeModel);
			storeUI.setContainerPosition();
			storeUI.createView();
			storeUI.setDepth(102);
			this.addChild(storeUI);
			
			this._storesUIArray.addObject(storeUI);
			
			this.reserveGridLocationForObject(storeUI);
		}
	},

	addFloor: function()
	{	
		if(this._floorsUIArray) {
			
			if(this._dummyFloor)
			{
				this._dummyFloor.destroy();
				this._dummyFloor = null;
			}
			MMSoundManager.playEffect(NEW_AND_UPGRADE_SOUND);
			var floorModel = this._mallObj.addFloor();
			var floorUI = new FloorUI(floorModel);
			var positionY = 0;
			positionY = positionY - this.getNodeHeight() - FLOOR_HEIGHT;
			
			floorUI.createView();
			floorUI.setDepth(101);
			floorUI.startConstruction();
			floorUI.setPosition([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(positionY)]);
			
			this.addChild(floorUI);
			this._floorsUIArray.addObject(floorUI);
			
			this.addGridRow();
			ElevatorHandler.addMoreElevatorsFloor();
			
			this._touchHandler.updateTouchTarget(this._floorsUIArray.objectAtIndex(this._floorsUIArray.getLength()-1).getPosition());
		}
	},
	
	getMallObj: function()
	{
		return this._mallObj;
	},
	
	addDummyFloor: function()
	{
		if(!this._dummyFloor)
		{
			var floorModel = new MMFloorModel();
			var floorUI = new FloorUI(floorModel);
			var positionY = 0;
			positionY = positionY - this.getNodeHeight() - FLOOR_CONSTRUCTION_HEIGHT;
			
			floorUI.createView();
			floorUI.setDepth(101);
			floorUI.setPosition([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(positionY)]);
			
			this.addChild(floorUI);
			this._dummyFloor = floorUI;
		} 
		else 
		{
			var positionY = 0;
			positionY = positionY - this.getNodeHeight() - FLOOR_CONSTRUCTION_HEIGHT;
			this._dummyFloor.setPosition([TCUtil.getAdjustedX(0), TCUtil.getAdjustedY(positionY)]);
		}
		this.handleEmptyShops();
	},
	
	getDummyFloor: function()
	{
		return this._dummyFloor;
	},
	
	addGridRow: function()
	{
		if(this._gridArray)
		{
			var index = this._gridArray.length;
			this._gridArray[index] = new Array();
		}
	},
	
	reserveGridLocationForObject: function(uiObject)
	{
		if(uiObject)
		{
			var objectModel = uiObject.getModelObject();
			if(objectModel)
			{
				var itemModel = objectModel.getItemModel();
				if(itemModel)
				{
					var itemSizeWidth = itemModel.custom_attributes.width;
					for(var i=0 ; i<itemSizeWidth ; i++)
					{
						var existingUI = this._gridArray[objectModel.getPosition().getX()][(objectModel.getPosition().getY())+i];
						if(existingUI)
						{
							existingUI.destroy();
							existingUI = null;
						}
						this._gridArray[objectModel.getPosition().getX()][(objectModel.getPosition().getY())+i] = uiObject;
					}
				}
			}
		}
	},
	
	handleStoreTapAction: function(position)
	{
		if(!DatabaseInterface._isVisitingNeighbor)
		{
			if(position)
			{
				var storeContainer = this._gridArray[position.getX()][position.getY()];	
				var itemId = storeContainer.getModelObject().getItemId();	
				var storeItem = GAGameLoader.getStoreHandler().getStoreObjectForKey(MMGameVariables.getPurchasedItemId());
				if(MMGameVariables.getPurchasedItemId() && MMGameVariables.getActionType() == kActionType.kActionBuy && TCUtil.getItemType(storeItem) == kItemType.kItemStore)
				{
					if(storeContainer && itemId == "1_1_0")
					{
						var purchasedItemId = MMGameVariables.getPurchasedItemId();
						var itemModel = GAGameLoader.getStoreHandler().getStoreObjectForKey(purchasedItemId);
						
						var priceForBucket = itemModel.buy_prices["default"];            
				        var defaultBuyPrice = TCUtil.getPrice(priceForBucket);    	
				    	var currencyType = TCUtil.getCurrency(priceForBucket);
						var user = MMGameState.getCurrentUser();
//				    	var isValidPurchase = user.isValidPurchase(currencyType, defaultBuyPrice);
//				    	
//				    	if(isValidPurchase)
//				    	{	
				    	var canItemFit = this.canItemFitAtPosition(purchasedItemId, position)
				    	if(canItemFit)
				    	{
				    		var operation = null;
					    	if(currencyType == kCurrencyType.kCurrencyTypeNuggets)
					    	{
					    		operation = kOperationType.kOperationSubtractNuggets;
					    	}
					    	else
					    	{
								operation = kOperationType.kOperationSubtractCoins;
					    	} 
					    			    	
					    	user.updateCurrency(operation, defaultBuyPrice);
					    	
							this.addStore(purchasedItemId, position);
							MMSoundManager.playEffect(NEW_AND_UPGRADE_SOUND);
							AnalyticsHandler.logAnalyticsTransaction(purchasedItemId);
							if(!TutorialManager.getHasCompletedTutorial())
							{
								if(TutorialManager.getCurrentTutorialStep() == 10)
								{
									TutorialManager.unhideTutorialPopup();
									TutorialManager.showNextTutorialStep();
									
								}
							}
							MMGameVariables.setPurchasedItemId(null);
							MMGameVariables.setActionType(kActionType.kActionArrow);
							MMGameLayer.getControlLayer().removeActionAlertWindow();
				    	}
//				    	}	
//				    	else
//				    	{
//				    		var alertBox = UIFactory.getAlertBox("Message", "You dont have enough cash to place "+itemModel.name+".", ["ok"], this);
//				    	}					
					}
				}
			
				else if(MMGameVariables.getActionType() == kActionType.kActionArrow)
				{
					if(TutorialManager.getHasCompletedTutorial())
					{
						if(itemId == "1_1_0")
						{
							var storeId = '1';
							var categoryId = '1';
							this._storeMainView =  new StoreMainView(storeId, categoryId);
							this._storeMainView.createView();
							NGWindow.document.addChild(this._storeMainView);
						}
						else 
						{
							var storeAttrbuteWindow = new MMStoreAttributeWindow(storeContainer);
							storeAttrbuteWindow.createView();
							MMGameLayer.getControlLayer().addChild(storeAttrbuteWindow);
						}
					}
					else 
					{
						if(itemId == "1_1_0")
						{
							
						}
						else
						{
							if(TutorialManager.getCurrentTutorialStep() == 2 || TutorialManager.getCurrentTutorialStep() == 3 || TutorialManager.getCurrentTutorialStep() == 4 || TutorialManager.getCurrentTutorialStep() == 5)
							{
								var storeAttrbuteWindow = new MMStoreAttributeWindow(storeContainer);
								storeAttrbuteWindow.createView();
								MMGameLayer.getControlLayer().addChild(storeAttrbuteWindow);
							}
						}
					}
				}
				
				else if (MMGameVariables.getPurchasedItemId() && MMGameVariables.getActionType() == kActionType.kActionUseGift &&  TCUtil.getItemType(storeItem) == kItemType.kItemStore)
				{
					//console.log("zaki::::::::::: IN ACTION PURCHASED");
					if(storeContainer && itemId == "1_1_0")
					{
						var purchasedItemId = MMGameVariables.getPurchasedItemId();
						var canItemFit = this.canItemFitAtPosition(purchasedItemId, position);
				    	if(canItemFit)
				    	{
							this.addStore(purchasedItemId, position);
							MMGameState.getCurrentUser().removeGift(MMGameVariables.getPurchasedItem());
							//AnalyticsHandler.logAnalyticsTransaction(purchasedItemId);
													
							MMGameVariables.setPurchasedItemId(null);
							MMGameVariables.setActionType(kActionType.kActionArrow);
							MMGameLayer.getControlLayer().removeActionAlertWindow();
				    	}
					}
				}
			}
		}
	},
	
	canItemFitAtPosition: function(itemId, position)
	{
		var itemModel = GAGameLoader.getStoreHandler().getStoreObjectForKey(itemId);
		var itemSizeWidth = itemModel.custom_attributes.width;
		
		if(itemSizeWidth > 1)
		{
			for(var i=0 ; i<3 ; i++)
			{
				var storeContainer = this._gridArray[position.getX()][i];
				var gridItemId = storeContainer.getModelObject().getItemId();
				if(gridItemId != '1_1_0')
				{
					return false;
				}
			}
		}
		else
		{
			var storeContainer = this._gridArray[position.getX()][position.getY()];
			var gridItemId = storeContainer.getModelObject().getItemId();
			if(gridItemId != '1_1_0')
			{
				return false;
			}
		}
		return true;
		
	},
	
	getStoreUIForGridPosition: function(x,y)
	{
		//console.log("zaki::: position recieved is X: "+x+"   Y:"+y);
		var storeUI = null;
		if(this._gridArray[x])
		{
			var storeContainer = this._gridArray[x][y];
			//console.log("zaki::: test 1");
			if(storeContainer)
			{
				//console.log("zaki::: test 2");
				var storeUIArray = storeContainer.getStoreUIArray();
				//console.log("zaki::: test 3");
				var storeUICount = storeUIArray.getLength();
				//console.log("zaki::: test 4");
				var storeItemId = storeContainer.getModelObject().getItemId();
				//console.log("zaki::: test 5");
				if(storeItemId != "1_1_0")
				{
					//console.log("zaki::: test 6");
					if(storeUICount > 1)
					{
						//console.log("zaki::: test 7");
						storeUI = storeUIArray.objectAtIndex(x);
					}
					else
					{
						//console.log("zaki::: test 8");
						storeUI = storeUIArray.objectAtIndex(0)
					}
				}
				else
				{
					//console.log("zaki::: test 9 item id is 1_1_0");
				}
			}
		}
		return storeUI;
	},
	
	getNodeHeight: function() 
	{
		return FLOOR_HEIGHT * this._floorsUIArray.getLength();
	},
	
	getFloorsUIArray: function()
	{
		return this._floorsUIArray;
	},
	
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/environmentlayer/";
	},
	
	zoomOutView: function ()
	{
		this.setScale(0.3,0.3);
	},
								   
	zoomInView: function ()
	{
		this.setScale(1,1);
	},
								   
	getMallObj: function()
	{
		if(this._mallObj)
		{
			return this._mallObj;
		}
		return null;
	},
	
	getTouchHandler: function()
	{
		if(this._touchHandler)
		{
			return this._touchHandler;
		}
	},
	
	destroyMallUIObjects: function()
	{
		TrashHandler.clearAddTrashTimer();
		AvatarHandler.clearAddAvatarTimer();
		TrashHandler.destroyTrashUIObjects();				
		AvatarHandler.destroyAvatarUIObjects();
		ElevatorHandler.destroyElevatorUIObjects();
		if(this._storesUIArray)
		{
			this._storesUIArray.destroyWithObjects();
			this._storesUIArray = null;
		}
		
		if(this._floorsUIArray)
		{
			this._floorsUIArray.destroyWithObjects();
			this._floorsUIArray = null;
		}
	},
								   
	destroy: function()
    {
    	this.destroyMallUIObjects();
    	if(this._gridArray)
		{
													
			for(var i = 0; i< this._gridArray.length; i++)
			{
				delete this._gridArray[i];
			}
			
			delete this._gridArray;
			this._gridArray = null;
		}
		if(this._mallObj) {
			delete this._mallObj;
		}
		if(this._storeMainView)
		{
			this._storeMainView.destroy();
			this._storeMainView = null;
		}
		if(this._floorsUIArray) {
			this._floorsUIArray.destroyWithObjects();
			this._floorsUIArray = null;
		}
		if(this._dummyFloor)
		{
			this._dummyFloor.destroy();
			this._dummyFloor = null;
		}
    },
});
